<!-- 系统行为日志编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改系统行为日志':'添加系统行为日志'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
                    
      <el-form-item
        label="操作人用户名:"
        prop="username">
        <el-input
          :maxlength="20"
          v-model="form.username"
          placeholder="请输入操作人用户名"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="请求类型:"
        prop="method">
        <el-input
          :maxlength="20"
          v-model="form.method"
          placeholder="请输入请求类型"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="模型:"
        prop="module">
        <el-input
          :maxlength="20"
          v-model="form.module"
          placeholder="请输入模型"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="操作方法:"
        prop="action">
        <el-input
          :maxlength="20"
          v-model="form.action"
          placeholder="请输入操作方法"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="操作页面:"
        prop="url">
        <el-input
          :maxlength="20"
          v-model="form.url"
          placeholder="请输入操作页面"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="请求参数(JSON格式):"
        prop="param">
        <el-input
          :maxlength="20"
          v-model="form.param"
          placeholder="请输入请求参数(JSON格式)"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="日志标题:"
        prop="title">
        <el-input
          :maxlength="20"
          v-model="form.title"
          placeholder="请输入日志标题"
          clearable/>
      </el-form-item>
                      
      <el-form-item label="操作类型:" prop="type">
        <el-select
          clearable
          class="ele-block"
          v-model="form.type"
          placeholder="请选择操作类型">
                  <el-option label="登录系统" :value="1"/>
                  <el-option label="注销系统" :value="2"/>
                  <el-option label="操作日志" :value="3"/>
                </el-select>
      </el-form-item>
                              
      <el-form-item
        label="IP地址:"
        prop="ip">
        <el-input
          :maxlength="20"
          v-model="form.ip"
          placeholder="请输入IP地址"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="User-Agent:"
        prop="user_agent">
        <el-input
          :maxlength="20"
          v-model="form.user_agent"
          placeholder="请输入User-Agent"
          clearable/>
      </el-form-item>
                
      <el-form-item label="内容:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.content"
          placeholder="请输入内容"/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'ActionLog202308Edit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              username: [
          {required: true, message: '请输入操作人用户名', trigger: 'blur'}
        ],
                  method: [
          {required: true, message: '请输入请求类型', trigger: 'blur'}
        ],
                  module: [
          {required: true, message: '请输入模型', trigger: 'blur'}
        ],
                  action: [
          {required: true, message: '请输入操作方法', trigger: 'blur'}
        ],
                  url: [
          {required: true, message: '请输入操作页面', trigger: 'blur'}
        ],
                  param: [
          {required: true, message: '请输入请求参数(JSON格式)', trigger: 'blur'}
        ],
                  title: [
          {required: true, message: '请输入日志标题', trigger: 'blur'}
        ],
                  
        type: [
          {required: true, message: '请选择操作类型', trigger: 'blur'}
        ],
                          ip: [
          {required: true, message: '请输入IP地址', trigger: 'blur'}
        ],
                  user_agent: [
          {required: true, message: '请输入User-Agent', trigger: 'blur'}
        ],
                  content: [
          {required: true, message: '请输入内容', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/actionlog202308/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
